import { AlertTriangle, RefreshCw, Home } from 'lucide-react';
import { useNavigate } from 'react-router-dom';

interface ErrorFallbackProps {
  error?: Error;
  resetError?: () => void;
}

export const ErrorFallback: React.FC<ErrorFallbackProps> = ({ error, resetError }) => {
  const navigate = useNavigate();

  const handleRetry = () => {
    if (resetError) {
      resetError();
    } else {
      window.location.reload();
    }
  };

  const handleGoHome = () => {
    navigate('/dashboard');
  };

  return (
    <div className='min-h-screen flex items-center justify-center bg-gray-50 px-4'>
      <div className='max-w-md w-full text-center'>
        <div className='mb-6'>
          <div className='mx-auto w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mb-4'>
            <AlertTriangle className='w-8 h-8 text-red-600' />
          </div>
          <h1 className='text-2xl font-bold text-gray-900 mb-2'>出错了</h1>
          <p className='text-gray-600 mb-6'>应用程序遇到了一个错误。我们为此带来的不便深表歉意。</p>
        </div>

        {error && process.env.NODE_ENV === 'development' && (
          <div className='mb-6 p-4 bg-red-50 border border-red-200 rounded-lg text-left'>
            <h3 className='font-semibold text-red-800 mb-2'>错误详情:</h3>
            <pre className='text-xs text-red-700 overflow-auto max-h-32'>
              {error.message}
              {error.stack && '\n\n' + error.stack}
            </pre>
          </div>
        )}

        <div className='space-y-3'>
          <button
            onClick={handleRetry}
            className='w-full btn-primary flex items-center justify-center gap-2'
          >
            <RefreshCw className='w-4 h-4' />
            重试
          </button>

          <button
            onClick={handleGoHome}
            className='w-full btn-secondary flex items-center justify-center gap-2'
          >
            <Home className='w-4 h-4' />
            返回首页
          </button>
        </div>

        <div className='mt-6 text-sm text-gray-500'>如果问题持续存在，请联系技术支持。</div>
      </div>
    </div>
  );
};

export default ErrorFallback;
